# all images shall be in square or it will be stretched

from kivymd.uix.bottomnavigation import MDBottomNavigationItem
from kivymd.uix.label import MDLabel

from kivymd.uix.tab import MDTabsBase

from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.screen import MDScreen
from kivymd.uix.card import MDCard
from kivy.uix.button import Button


#from kivy.core.window import Window  # For debug use on Desktop
#Window.size = (607, 1080)


###################################################################################
# Back-end Functions

itemCount = [5, 4]
nums = [[], []]
for j in range(2):
    for i in range(itemCount[j]):
        nums[j].append(0)


def itemRemoveOne(itemType, itemNo):
    if nums[itemType][itemNo] > 0:
        nums[itemType][itemNo] = nums[itemType][itemNo]-1


def itemAddOne(itemType, itemNo):
    nums[itemType][itemNo] = nums[itemType][itemNo]+1


itemDicts = [
    # Ingredients
    [{'imgPath': 'shopImgs/ingredients01.jpg',
      'title': 'i1',
      'actualPrice': 50},
     {'imgPath': 'shopImgs/ingredients02.jpg',
      'title': 'i2',
      'actualPrice': 100}
     ],
    # Devices
    [{'imgPath': 'shopImgs/devices01.jpg',
      'title': 'd1',
      'actualPrice': 500}
     ]
]

##########################################################################
# Kivy Front-End


class TabShopIngredients(MDFloatLayout, MDTabsBase):
    def switchToDetail(self,destName):
        print(self.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent)
        self.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.transition.direction = 'left'
        self.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.parent.current = 'scrItem'
        print('Screen switched to Item!')




class TabShopDevices(MDFloatLayout, MDTabsBase):
    pass


class ShopMainScreen(MDBottomNavigationItem):
   # def __init__(self, **kw):
    #    super().__init__(**kw)
    #   self.ids.tabShop.add_widget(TabShopIngredients())
    # self.ids.tabShop.add_widget(TabShopDevices())

    def itemRemoveOne_(self, itemType, itemNo):
        itemRemoveOne(itemType, itemNo)

    def itemAddOne_(self, itemType, itemNo):
        itemAddOne(itemType, itemNo)

    def switchToDetail(self):
        self.parent.parent.parent.parent.transition.direction = 'left'
        self.parent.parent.parent.parent.current = 'scrItem'
        print('screen switched to Item!')

    def switchToChkOut(self):

        # this is the screenmanager we're looking for
        print(self.parent.parent.parent.parent.manager)

        self.parent.parent.parent.parent.manager.transition.direction = 'left'
        self.parent.parent.parent.parent.manager.current = 'scrChkOut'
        print('screen switched to CO!')

    def switchBack(self):
        # switch back to main
        pass


class ShopItemScreen(MDScreen):
    def switchBack(self):
        self.parent.transition.direction = 'right'
        self.parent.current = 'scrMain'
        print('screen switched to Main!')

    def switchToChkOut(self):
        self.parent.transition.direction = 'left'
        self.parent.current = 'scrChkOut'  
        print('screen switched to CO!')

    def refreshItemCountDisplay(self):
        self.ids.itemCountDisplay.text = str(nums[self.itemType][self.itemNo])

    def on_enter(self):
        self.refreshItemCountDisplay()

    def itemRemoveOne_(self, itemType, itemNo):
        itemRemoveOne(itemType, itemNo)
        self.refreshItemCountDisplay()

    def itemAddOne_(self, itemType, itemNo):
        itemAddOne(itemType, itemNo)
        self.refreshItemCountDisplay()


class CheckOutScreen(MDScreen):
    def switchBack(self):
        print("self.parent:  ")
        print(self.parent)
        self.parent.transition.direction = 'right'
        self.parent.current = 'scrMain'
        print('screen switched to Main!')

    def switchToFinal(self):
        self.parent.transition.direction = 'left'
        self.parent.current = 'scrFinal'
        print('screen switched to Final!')
        return 0

    def on_leave(self):
        self.ids.ChkOutLayout.clear_widgets()

    def on_enter(self):
        cardTotalNum = 0
        for iType in range(2):
            for i in range(itemCount[iType]):
                if nums[iType][i] != 0:
                    cardTotalNum += 1
                    widgetToAdd = ChkOutCard()
                    widgetToAdd.imgPath = itemDicts[iType][i]['imgPath']
                    widgetToAdd.title = itemDicts[iType][i]['title']
                    widgetToAdd.actualPrice = itemDicts[iType][i]['actualPrice']
                    widgetToAdd.quantity = nums[iType][i]
                    widgetToAdd.itemType = iType
                    self.ids.ChkOutLayout.add_widget(widgetToAdd)

        self.ids.ChkOutLayout.size_hint_y = (cardTotalNum+2)/5 - 0.15

        newbtn = Button(
            halign='center',
            text="FINISH",
            background_color=(0.827, 0.659, 0.071, 1),
            background_normal='',
            size_hint_x=1)

        if cardTotalNum == 0:
            self.ids.ChkOutLayout.add_widget(MDLabel(
                font_style='Subtitle2',
                halign='center',
                text_color=(0.1, 0.1, 0.1, 1),
                text="You Don't have any items here yet."
            ))
            newbtn.text = "GO BACK"
            newbtn.bind(on_press=lambda x: self.switchBack())

        else:
            newbtn.text = 'FINISH'
            newbtn.bind(on_press=lambda x: self.switchToFinal())

        self.ids.ChkOutLayout.add_widget(newbtn)


class FinalScreen(MDScreen):
    pass


class ShopCard(MDCard):
    # MDCard Classes must start with Capitalized letters
    """def __init__(self, **kw):
        super().__init__(**kw)
        self.imgPath = itemDicts[itemType][itemNo]['imgPath']
        self.title = itemDicts[itemType][itemNo]['title']
        self.actualPrice = itemDicts[itemType][itemNo]['actualPrice']
        self.quantity = nums[itemType][itemType]
        self.itemType = itemType
        self.itemNo = itemNo"""
    pass


class ChkOutCard(MDCard):
    pass


"""
class MainApp(MDApp):

    def build(self):
        self.root = Builder.load_file("shop_main.kv")  # the path to widgets
        scrmgr = ScreenManager()
        scrmgr.add_widget(ShopMainScreen(name='scrMain'))
        scrmgr.add_widget(ShopItemScreen(name='scrItem'))
        scrmgr.add_widget(CheckOutScreen(name='scrChkOut'))
        scrmgr.add_widget(FinalScreen(name='scrFinal'))
        return scrmgr


"""
###################################################################################
# MainApp().run()
